package com.yjz.ui.util

import android.graphics.Color

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/5/15 16:41
 */
object ColorUtil {

    /**
     * 根据开始颜色和结束颜色，以及百分比来计算开始和结束的中间颜色
     * @param startColor 开始颜色
     * @param endColor 结束颜色
     * @param fraction 当前百分比
     */
    fun getCurrentColor(startColor: Int, endColor: Int, fraction: Float): Int {
        //endColor(argb) - startColor（argb）= diffrence(argb)
        //startColor（argb) + diffrence(argb) * fraction = newColor
        val redStart = Color.red(startColor)
        val blueStart = Color.blue(startColor)
        val greenStart = Color.green(startColor)
        val alphaStart = Color.alpha(startColor)

        val redEnd = Color.red(endColor)
        val blueEnd = Color.blue(endColor)
        val greenEnd = Color.green(endColor)
        val alphaEnd = Color.alpha(endColor)

        val redDiff = redEnd - redStart
        val blueDiff = blueEnd - blueStart
        val greenDiff = greenEnd - greenStart
        val alphaDiff = alphaEnd - alphaStart

        val redCurrent = (redStart + fraction * redDiff).toInt()
        val blueCurrent = (blueStart + fraction * blueDiff).toInt()
        val greenCurrent = (greenStart + fraction * greenDiff).toInt()
        val alphaCurrent = (alphaStart + fraction * alphaDiff).toInt()
        return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent)
    }

}